www.gusucode.com > VC++ 自制窗口标题(caption)程序 > VC++ 自制窗口标题(caption)程序/code/CustomCaption/CaptionBackground.cpp

    #include "stdafx.h"
#include "CaptionBackground.h"

//////////////////
// Helper to paint rectangle with a color.
//
CCaptionBackground::CCaptionBackground()
	:m_ActiveColor(-1), m_InactiveColor(-1)
{
}

CCaptionBackground::CCaptionBackground(const CCaptionBackground& cb)
	:m_ActiveColor(cb.m_ActiveColor), m_InactiveColor(cb.m_InactiveColor)
{
}

void CCaptionBackground::SetCustomColors(const CCaptionBackground& cb)
{
	SetActiveColor(cb.m_ActiveColor);
	SetInactiveColor(cb.m_InactiveColor);
}

void CCaptionBackground::SetCustomColors(COLORREF activeColor, COLORREF inactiveColor)
{
	SetActiveColor(activeColor);
	SetInactiveColor(inactiveColor);
}

void CCaptionBackground::SetActiveColor(COLORREF activeColor)
{
	m_ActiveColor = activeColor;
}

void CCaptionBackground::SetInactiveColor(COLORREF inactiveColor)
{
	m_InactiveColor = inactiveColor;
}

void CCaptionBackground::UseSystemColors()
{
	UseSystemActiveColor();
	UseSystemInactiveColor();
}

void CCaptionBackground::UseSystemActiveColor()
{
	m_ActiveColor = -1;
}

void CCaptionBackground::UseSystemInactiveColor()
{
	m_InactiveColor = -1;
}

COLORREF CCaptionBackground::GetActiveColor()	const
{
	return m_ActiveColor == -1 ? GetSysColor(COLOR_ACTIVECAPTION) : m_ActiveColor;
}

COLORREF CCaptionBackground::GetInactiveColor() const
{
	return m_InactiveColor == -1 ? GetSysColor(COLOR_INACTIVECAPTION) : m_InactiveColor;
}

void CCaptionBackground::Paint(CDC* pDC, CSize paintArea, BOOL active)
{
	PaintRect(pDC, 0, 0, paintArea.cx, paintArea.cy, active ? GetActiveColor() : GetInactiveColor());
}

// Paint the color into the rectangle dimensions on the device context
void CCaptionBackground::PaintRect(CDC* pDC, int x, int y, int w, int h, COLORREF color)
{
	CBrush brush(color);
	CBrush* pOldBrush = pDC->SelectObject(&brush);
	pDC->PatBlt(x, y, w, h, PATCOPY);
	pDC->SelectObject(pOldBrush);
}